﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jb.Domain;
using Jb.Common.Extensions;
using System.Reflection;


namespace Jb.Web.Utility
{
    public class BindSelectList
    {   
        //public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
        //    where T : class
        //{            
        //    SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
        //    SelectListItem item = new SelectListItem();            
        //    return selectList;            
        //}
        /// <summary>
        /// Binds a Collection to the Select List. Adds a new entry at first as (Please Select with blank value )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="itemList"></param>
        /// <param name="dataValueField"></param>
        /// <param name="dataTextField"></param>
        /// <returns></returns>
        public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
            where T : class
        {
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            PropertyInfo displayProperty = type.GetProperty(dataTextField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Text Field Property {0} was not found.", dataTextField));
            else
                displayProperty.SetValue(obj, "Please Select", null);

            displayProperty = type.GetProperty(dataValueField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Value Field Property {0} was not found.", dataValueField));
            else
                displayProperty.SetValue(obj, 0, null);

            if (itemList == null)
                itemList = new List<T>();

            itemList.Insert(0, (T)obj);
            SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
            return selectList;
        }

        //public static SelectList Manufacturers()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Manufacturer> manufacturers = context.Manufacturers.OrderBy(P => P.Name).ToList();
        //    return Bind<Manufacturer>(manufacturers, "ID", "Name");
        //}
        /// <summary>
        /// Binds Job Categories
        /// </summary>
        /// <returns></returns>
        public static SelectList Categories()
        {
            JbEntities context = new JbEntities();
            List<Category> categories = (from P in context.Categories orderby P.Name select P).ToList();            
            return Bind<Category>(categories, "ID", "Name");
        }
        //public static SelectList ParentCategories()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Category> categories = (from P in context.Categories where P.ParentID == null select P).ToList(); 
            
        //    return Bind<Category>(categories, "ID", "Name");
        //}
        /// <summary>
        /// Binds Industry Types
        /// </summary>
        /// <returns></returns>
        public static SelectList IndustryTypes()
        {
            JbEntities context = new JbEntities();
            List<IndustryType> types = context.IndustryTypes.OrderBy(P => P.Name).ToList();
            return Bind<IndustryType>(types, "ID", "Name");
        }
        /// <summary>
        /// Binds Countries
        /// </summary>
        /// <returns></returns>
        public static SelectList Countries()
        {
            JbEntities context = new JbEntities();
            List<Country> types = context.Countries.OrderBy(P => P.Name).ToList();
            return Bind<Country>(types, "ID", "Name");
        }
        /// <summary>
        /// Binds States by Country
        /// </summary>
        /// <param name="countryID"></param>
        /// <returns></returns>
        public static SelectList States(int countryID)
        {
            JbEntities context = new JbEntities();
            List<State> types = (from P in context.States where P.CountryID == countryID orderby P.Name select P).ToList();
            return Bind<State>(types, "ID", "Name");
        }
        /// <summary>
        /// Binds Cities By State
        /// </summary>
        /// <param name="stateID"></param>
        /// <returns></returns>
        public static SelectList CitiesByState(int stateID)
        {
            JbEntities context = new JbEntities();
            List<City> types = (from P in context.Cities where P.StateID == stateID orderby P.Name select P).ToList();
            return Bind<City>(types, "ID", "Name");
        }
        /// <summary>
        /// Binds Cities By Country
        /// </summary>
        /// <param name="countryID"></param>
        /// <returns></returns>
        public static SelectList CitiesByCountry(int countryID)
        {
            JbEntities context = new JbEntities();
            //var states = from C in context.States where C.CountryID == countryID select C.ID;
            var query = from P in context.Cities
                        where (from C in context.States where C.CountryID == countryID select C.ID).Contains(P.StateID)
                        select P;
            List<City> types = new List<City>();
            if (query != null && query.Count() > 0)
                types = query.ToList();
            return Bind<City>(types, "ID", "Name");
        }        
        public static SelectList Gender()
        {
            var genderList = new SelectList(new[]
                            {
                                new {ID="",Name="Please Select"},
                                new {ID="M",Name="Male"},
                                new {ID="F",Name="Female"},
                                new {ID="O",Name="Other"},
                            }, "ID", "Name");  
            return genderList;
        }

        //public static SelectList Religions()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Religion> types = (from P in context.Religions orderby P.Name select P).ToList();
        //    return Bind<Religion>(types, "ID", "Name");
        //}

        public static SelectList PreferredPhone()
        {
            var phoneTypeList = new SelectList(new[]
                            {
                                new {ID="",Name="Please Select"},
                                new {ID="H",Name="Home"},
                                new {ID="M",Name="Mobile"},
                                new {ID="O",Name="Office"},
                            }, "ID", "Name");
            return phoneTypeList;
        }
        public static SelectList EmployeeType()
        {
            JbEntities context = new JbEntities();
            List<EmployeeType> types = (from P in context.EmployeeTypes orderby P.Name select P).ToList();
            return Bind<EmployeeType>(types, "ID", "Name");
        }

        public static MultiSelectList Educations()
        {
            JbEntities context = new JbEntities();
            List<Education> types = (from P in context.Educations select P).ToList();
            return new MultiSelectList(types, "ID", "Name");
        }

        public static MultiSelectList Skills(int categoryID)
        {
            JbEntities context = new JbEntities();
            List<Skill> types = (from P in context.Skills where P.CategoryID == categoryID select P).ToList();
            return new MultiSelectList(types, "ID", "Name");
        }

        public static SelectList JobLevels()
        {
            JbEntities context = new JbEntities();
            List<JobLevel> types = (from P in context.JobLevels orderby P.Name select P).ToList();
            return Bind<JobLevel>(types, "ID", "Name");
        }

        public static SelectList AddressTypes()
        {
            var query = from Jb.Web.ViewModels.AddressType e in Enum.GetValues(typeof(Jb.Web.ViewModels.AddressType))
                         select new NameValue{ ID = (int) e, Name = e.ToString() };            
            return Bind<NameValue>(query.ToList(), "ID", "Name");
        }

        public static SelectList Currencies()
        {
            JbEntities context = new JbEntities();
            List<Currency> types = (from P in context.Currencies orderby P.Name select P).ToList();
            return Bind<Currency>(types, "ID", "Name");
        }

        public static SelectList Resumes(int jobSeekerID)
        {
            JbEntities context = new JbEntities();
            List<Resume> types = (from P in context.Resumes where P.JobSeekerID == jobSeekerID select P).ToList();
            return Bind<Resume>(types, "ID", "Title");
        }

        /// <summary>
        /// Gets the Coverletters for a Job Seeker and sets the default coverletter ID.        
        /// </summary>
        /// <param name="jobSeekerID"></param>
        /// <param name="defaultCoverLetterID"></param>
        /// <returns></returns>
        public static SelectList CoverLetters(int jobSeekerID)//, out int defaultCoverLetterID)
        {
            //defaultCoverLetterID = 0;
            JbEntities context = new JbEntities();
            List<CoverLetter> types = (from P in context.CoverLetters where P.JobSeekerID == jobSeekerID select P).ToList();
            //foreach (CoverLetter coverLetter in types)
            //{
            //    if (coverLetter.IsDefault)
            //        defaultCoverLetterID = coverLetter.ID;
            //}
            return Bind<CoverLetter>(types, "ID", "Title");
        }

        /// <summary>
        /// Binds a 24 Hour Selectlist without 'Please Select' element
        /// </summary>
        /// <returns></returns>
        public static SelectList Hours()
        {
            List<NameValue> hours = new List<NameValue>();
            hours.Add(new NameValue { ID = -1, Name = "Hour" });
            for (int i = 0; i < 24; i++)
            {
                hours.Add(new NameValue { ID = i, Name = i.ToString().PadLeft(2, '0') });
            }
            return new SelectList(hours, "ID", "Name");
        }

        /// <summary>
        /// Binds a Minutes SelectList without the 'Please Select' element
        /// </summary>
        /// <returns></returns>
        public static SelectList Minutes()
        {
            List<NameValue> minutes = new List<NameValue>();
            minutes.Add(new NameValue { ID = -1, Name = "Minute" });
            for (int i = 0; i < 60; i++)
            {
                minutes.Add(new NameValue { ID = i, Name = i.ToString().PadLeft(2, '0') });
            }
            return new SelectList(minutes, "ID", "Name");
        }

        /// <summary>
        /// Salary Range Dropdownlist
        /// Used only in Advanced Search Form, not involving any other business process.
        /// </summary>
        /// <returns></returns>
        public static SelectList SalaryRange()
        {
            List<NameValue> salaryRange = new List<NameValue>();
            salaryRange.Add(new NameValue { ID = -1, Name = "Please Select" });
            String[] identifier = SiteConfigReader.SalaryRangeIdentifier.Split(':');
            int lastRange = identifier[0].ToInt();
            for (int i = 1; i < 11; i++)
            {                
                salaryRange.Add(new NameValue { ID=lastRange, Name = String.Format("{0} {1}+", ConfigReader.CurrencyCode, String.Format(AppConstants.ValueOf.DECIMAL_FORMAT, lastRange)) });
                lastRange = lastRange + identifier[1].ToInt();   
            }
            return new SelectList(salaryRange, "ID", "Name");
        }

        /// <summary>
        /// Generates Postdate Dropdownlist for Advanced Search Page
        /// </summary>
        /// <returns></returns>
        public static SelectList PostDates()
        {
            List<NameValue> dates = new List<NameValue>();
            dates.Add(new NameValue { ID = -1000, Name = "Any Date" });
            dates.Add(new NameValue { ID = +0, Name = "Today" });
            dates.Add(new NameValue { ID = -1, Name = "Yesterday" });
            dates.Add(new NameValue { ID = -7, Name = "Last 7 Days" });
            dates.Add(new NameValue { ID = -15, Name = "Last 15 Days" });
            dates.Add(new NameValue { ID = -30, Name = "Last 30 Days" });            
            return new SelectList(dates, "ID", "Name");
        }
    }

    public class NameValue
    {
        public int ID { get; set; }
        public String Name { get; set; }
    }
}